package br.ufpe.cin.msc.jrsj2.recommender.message;

import java.io.StringWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;

import br.ufpe.cin.msc.jrsj2.recommender.persistence.domain.Client;
import br.ufpe.cin.msc.jrsj2.recommender.util.ClientHash;

public class ClientMessage extends ApplicationMessage {

	public enum ClientMessageAction {
		LIST_CLIENT, CREATE_CLIENT, DELETE_CLIENT
	};

	private ClientMessageAction clientMessageAction;
	private String hash;

	public ClientMessageAction getClientMessageAction() {
		return this.clientMessageAction;
	}

	public String getHash() {
		return ClientHash.getClientHashString(hash);
	}

	@Override
	protected void parseMessageElement(String uri, String localName,
			String qName, Attributes attributes) {
		if (qName.equals("client")) {
			String action = attributes.getValue("action");
			if (action.equals("list")) {
				clientMessageAction = ClientMessageAction.LIST_CLIENT;
				responseMessage = "list";
				return;
			}
			if (action.equals("create")) {
				hash = attributes.getValue("hash");
				clientMessageAction = ClientMessageAction.CREATE_CLIENT;
				responseMessage = "create";
				return;
			}
			if (action.equals("delete")) {
				hash = attributes.getValue("hash");
				clientMessageAction = ClientMessageAction.DELETE_CLIENT;
				responseMessage = "delete";
				return;
			}
		}
	}

	@Override
	@SuppressWarnings("rawtypes")
	public void buildResponseMessage(List objects) {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();
		Document document;
		Node root;

		try {
			document = docFactory.newDocumentBuilder().newDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return;
		}
		root = document.createElement("client-list");
		document.appendChild(root);

		@SuppressWarnings("unchecked")
		List<Client> clients = objects;

		for (Client c : clients) {
			Element client = document.createElementNS(null, "client");
			client.setAttributeNS(null, "hash", c.getHash());
			root.appendChild(client);
		}

		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		StringWriter writer = new StringWriter();
		try {
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(document);
			StreamResult result = new StreamResult(writer);
			transformer.transform(source, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}

		ApplicationMessage.responseMessage = writer.toString();
	}

	@Override
	protected void parseMessageContent(char[] ch, int start, int length) {
	}

	@Override
	protected void parseMessageEndElement(String uri, String localName,
			String qName) {
	}

}
